package com.iraeta.service.basic.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iraeta.api.HikRegionApi;
import com.iraeta.common.ServiceException;
import com.iraeta.entity.TreeNode;
import com.iraeta.entity.basic.HikRegion;
import com.iraeta.entity.basic.type.HikRegionTreeNode;
import com.iraeta.mapper.basic.HikRegionMapper;
import com.iraeta.service.basic.IHikRegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 海康的区域表 服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2024-01-10
 */
@Service
public class HikRegionServiceImpl extends ServiceImpl<HikRegionMapper, HikRegion> implements IHikRegionService {
    @Autowired
    HikRegionApi regionApi;

    @Override
    public HikRegion getRootHikRegion() {
        QueryWrapper<HikRegion> qw = new QueryWrapper<>();
        qw.eq("parentindexcode", "-1");
        return baseMapper.selectOne(qw);
    }

    @Override
    public HikRegion getHikRegion(String indexCode) {
        QueryWrapper<HikRegion> qw = new QueryWrapper<>();
        qw.eq("indexcode", indexCode);
        return baseMapper.selectOne(qw);
    }

    @Override
    public void addEditHikRegion(HikRegion hikRegion) {
        HikRegion hikRegionDb = getHikRegion(hikRegion.getIndexcode());
        if(hikRegionDb == null){
            baseMapper.insert(hikRegion);
        }else {
            hikRegion.setId(hikRegionDb.getId());
            baseMapper.updateById(hikRegion);
        }
    }

    @Override
    public void updateHikRegionFromHikIsc() throws Exception {
        HikRegion rootHikRegion = this.getRootHikRegion();
        String hikApiRes = regionApi.getChildrenRegion(rootHikRegion.getIndexcode());
        JSONObject jsonObject = JSON.parseObject(hikApiRes);
        JSONArray regionJSONArray = jsonObject.getJSONArray("list");
        for(int i = 0; i<regionJSONArray.size(); i++){
            JSONObject regionJSONObject = regionJSONArray.getJSONObject(i);
            String name = regionJSONObject.getString("name");
            String indexCode = regionJSONObject.getString("indexCode");
            String parentIndexCode = regionJSONObject.getString("parentIndexCode");
            int sort = regionJSONObject.getIntValue("sort");
            HikRegion hikRegion = new HikRegion(name, indexCode, parentIndexCode, sort);
            this.addEditHikRegion(hikRegion);
        }
    }

    @Override
    public List<HikRegionTreeNode> getHikRegionTableNode() {
        QueryWrapper<HikRegion> qw = new QueryWrapper<>();
        qw.orderByAsc("sort");
        List<HikRegion> hikRegionList = baseMapper.selectList(qw);
        HikRegion hikRegion = this.getRootHikRegion();

        ArrayList<HikRegionTreeNode> res = new ArrayList<>();
        HikRegionTreeNode hikRegionTreeNode = new HikRegionTreeNode();
        BeanUtil.copyProperties(hikRegion, hikRegionTreeNode);
        getChildrenHikRegionLoop(hikRegionTreeNode, hikRegionList);
        res.add(hikRegionTreeNode);
        return res;
    }
    private void getChildrenHikRegionLoop(HikRegionTreeNode treeNode, List<HikRegion> hikRegionTreeNodeList){
        List<HikRegion> childHikRegionList = hikRegionTreeNodeList.stream().filter(x-> x.getParentindexcode().equals(treeNode.getIndexcode())).collect(Collectors.toList());
        for(HikRegion childNode : childHikRegionList){
            HikRegionTreeNode childTreeNode = new HikRegionTreeNode();
            childTreeNode.setId(childNode.getId());
            childTreeNode.setIndexcode(childNode.getIndexcode());
            childTreeNode.setName(childNode.getName());
            treeNode.addNode(childTreeNode);
            getChildrenHikRegionLoop(childTreeNode, hikRegionTreeNodeList);
        }
    }

    @Override
    public List<TreeNode> getHikRegionTreeNode() {
        QueryWrapper<HikRegion> qw = new QueryWrapper<>();
        qw.orderByAsc("sort");
        List<HikRegion> hikRegionList = baseMapper.selectList(qw);
        HikRegion rootHikRegion = this.getRootHikRegion();

        ArrayList<TreeNode> res = new ArrayList<>();
        TreeNode rootTreeNode = new TreeNode();
        rootTreeNode.setId(rootHikRegion.getIndexcode());
        rootTreeNode.setpId(rootHikRegion.getParentindexcode());
        rootTreeNode.setLabel(rootHikRegion.getName());
        getChildrenHikRegionLoop(rootTreeNode, hikRegionList);
        res.add(rootTreeNode);
        return res;
    }
    private void getChildrenHikRegionLoop(TreeNode treeNode, List<HikRegion> hikRegionList){
        List<HikRegion> childHikRegionList = hikRegionList.stream().filter(x-> x.getParentindexcode().equals(treeNode.getId())).collect(Collectors.toList());
        for(HikRegion childNode : childHikRegionList){
            TreeNode childTreeNode = new TreeNode(childNode.getIndexcode(), childNode.getName());
            treeNode.addNode(childTreeNode);
            getChildrenHikRegionLoop(childTreeNode, hikRegionList);
        }
    }
}
